package chenshuaiwen.service.collection;

import chenshuaiwen.domain.dto.RoleDto;
import chenshuaiwen.domain.entity.Role;
import chenshuaiwen.domain.vo.Result;
import chenshuaiwen.domain.vo.RoleVo;
import chenshuaiwen.service.IRoleService;
import chenshuaiwen.service.IUserService;
import chenshuaiwen.util.SingletonFactory;

import java.time.LocalDateTime;


/**
 * 业务层-部门
 *
 * @author 陈帅闻
 * @since 2025/4/20 15:19
 */
@Deprecated
public class RoleService extends BaseService<Role, RoleDto, RoleVo> implements IRoleService{

    private IUserService userService;

    private RoleService(){
        temp = new RoleVo[0];
    }

    @Override
    RoleVo convertToVo(Role role){
        return new RoleVo(role.getId(), role.createAt, role.updateAt, role.name, role.dataScope);
    }


    @Override
    Role createEntity(RoleDto dto,int id){
        LocalDateTime now = LocalDateTime.now();
        return new Role(id, now, now, dto.name,dto.dataScope);
    }

    @Override
    void updateEntity(RoleDto dto,Role role){
        role.updateAt = LocalDateTime.now();
        role.name = dto.name;
        role.dataScope = dto.dataScope;
    }

    @Override
    public Result delete(int id) {
        if(!existsById(id)){
            return Result.failure("删除失败，不存在该数据");
        }
        if(userService == null){
            userService = SingletonFactory.getInstance(UserService.class);
        }
        if(userService.roleInUse(id)){
            return Result.failure("删除失败，还有用户具有该角色，不能删除");
        }
        data.remove(id);
        return Result.success("删除成功", null);
    }
}
